/**
 * @project name:crm
 */
package com.yunbo.crm.controller;

import com.yunbo.crm.base.BaseController;
import com.yunbo.crm.base.ResultInfo;
import com.yunbo.crm.pojo.SaleChance;
import com.yunbo.crm.query.SaleChanceQuery;
import com.yunbo.crm.service.SaleChanceService;
import com.yunbo.crm.utils.LoginUserUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2021/1/21 13:40
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@RestController
@RequestMapping("sale_chance")
public class SaleChanceController extends BaseController {

    @Resource
    private SaleChanceService saleChanceService;

    /**
     * 营销机会多条件分页查询
     *
     * @param saleChanceQuery
     * @return
     */
    @GetMapping("list")
    public Map<String, Object> querySaleChanceList(Integer flag, SaleChanceQuery saleChanceQuery, HttpServletRequest request) {
        if (null != flag && flag == 1) {
            saleChanceQuery.setAssignMan(LoginUserUtil.releaseUserIdFromCookie(request).toString());
        }
        return saleChanceService.querySaleChanceByParams(saleChanceQuery);
    }

    @GetMapping("index")
    public ModelAndView toSaleChancePage(ModelAndView modelAndView) {
        modelAndView.setViewName("saleChance/sale_chance");
        return modelAndView;
    }

    @PostMapping("save")
    public ResultInfo addSaleChance(SaleChance saleChance, HttpServletRequest request) {
        saleChanceService.addSaleChance(saleChance, request);
        /**
         * base 里面 直接拿success
         */
        return success();
    }

    @PutMapping("update")
    public ResultInfo updateSaleChance(SaleChance saleChance, HttpServletRequest request) {
        saleChanceService.updateSaleChance(saleChance, request);
        return success();
    }

    @DeleteMapping("delete")
    public ResultInfo deleteSaleChance(Integer[] ids) {
        saleChanceService.deleteSaleChances(ids);
        return success();
    }

    @PutMapping("updateSaleChanceDevResult")
    public ResultInfo updateSaleChanceDevResult(Integer rowId, Integer devResult) {
        saleChanceService.updateSaleChanceDevResult(rowId, devResult);
        return success("开发状态更新成功");
    }


    @GetMapping("addOrUpdateSaleChancePage")
    public ModelAndView addOrUpdateSaleChancePage(SaleChance saleChance, ModelAndView modelAndView) {
        if (!Objects.isNull(saleChance.getRowId())) {
            saleChance = saleChanceService.selectByPrimaryKey(saleChance.getRowId());
            modelAndView.addObject("saleChance", saleChance);
        }
        modelAndView.setViewName("saleChance/add_update");
        return modelAndView;
    }

}
